package com.ailot.cloud.mall.service.impl;

import com.ailot.cloud.base.common.constants.CommonConstant;
import com.ailot.cloud.mall.dto.GoodsCategoryTree;
import com.ailot.cloud.mall.entity.GoodsCategory;
import com.ailot.cloud.mall.mapper.GoodsCategoryMapper;
import com.ailot.cloud.mall.service.GoodsCategoryService;
import com.ailot.cloud.upms.common.utils.TreeUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 品类
 *
 * @Author ailot
 * @create 2024-11-11
 */
@Service
public class GoodsCategoryServiceImpl extends ServiceImpl<GoodsCategoryMapper, GoodsCategory> implements GoodsCategoryService {

    @Override
    public List<GoodsCategoryTree> selectTree() {
        List<GoodsCategory> list = this.list();
        List<GoodsCategoryTree> treeList = list.stream()
                .map(GoodsCategoryTree::new)
                .sorted(Comparator.comparingInt(GoodsCategoryTree::getSort))
                .collect(Collectors.toList());
        return TreeUtil.build(treeList, CommonConstant.PARENT_ID);
    }
}
